# coding:utf-8

import datetime
import json


def toJsonString(data):
    if isinstance(data, list):
        return toJSonStringByList(data)
        pass


def toJSonStringByList(data):
    list = []
    for i in data:
        list.append(getDict(i))
    return json.dumps(list,ensure_ascii=False)


def getDict(data):
    user = dict(data.__dict__).copy()
    for key in list(user.keys()):
        if "_sa_instance_state" == key:
            user.pop(key)
            continue
        user[key]=dataToString(user[key])
    return user


def dataToString(data):
    if isinstance(data, datetime.datetime):
        return dateToString(data)
    else:
        return data




def dateToString(data):
    return data.strftime('%Y-%m-%d %H:%M:%S')
